package com.it.mydream.Controller;

import com.it.mydream.Bean.MsgBoard;
import com.it.mydream.Bean.User;
import com.it.mydream.Service.imp.LoginServiceImp;
import com.it.mydream.Service.imp.MsgBoardServiceImp;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.Part;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    private LoginServiceImp loginService;

    @Autowired
    private MsgBoardServiceImp msgBoardService;
    @RequestMapping("goLogin")
    public String goLogin(Model model){
        model.addAttribute("user",new User());
        return "login";
    }

    @RequestMapping("login")
    public String login(@ModelAttribute User user_t, Map<String, Object> map, HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = loginService.CheckUser(user_t);
        if(user != null){
            User user1 = loginService.getUserProFile(user);
            user_t.setUser_id(user.getUser_id());
            if(user1==null){
                user_t.setUser_img_path("/userimg/");
                user_t.setUser_img_name("undefined.jpg");
            }else{
                user_t.setUser_img_path(user1.getUser_img_path());
                user_t.setUser_img_name(user1.getUser_img_name());
            }
            //更新用户状态
            user_t.setUser_status("20003");
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            user_t.setUser_status_time(sdf.format(System.currentTimeMillis()));
            loginService.updateUserStatus(user_t);

            //获取留言板信息
            List<MsgBoard> msgBoards = (List<MsgBoard>) application.getAttribute("msgBoards");
            if(msgBoards==null){
                msgBoards = msgBoardService.GetMsgBoards();
                application.setAttribute("msgBoards",msgBoards);
            }
            session.setAttribute("user",user_t);
            List<User> userlist = loginService.getUserList();
            application.setAttribute("userlist",userlist);
            return "main/main";
        }
        return "login";
    }

    @RequestMapping("goSignup")
    public String goSignup(Model model){
        model.addAttribute("user",new User());
        return "signup";
    }

    @RequestMapping("signup")
    public String signup(@ModelAttribute User user_t,HttpServletRequest request){
        loginService.SignUser(user_t);
        User user = loginService.CheckUser(user_t);
        user_t.setUser_id(user.getUser_id());
        user_t.setUser_status("20001");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user_t.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.insertUserStatus(user_t);

        //设置初始头像地址 以防后续通过头像查不到数据
        user.setUser_img_path("/userimg/");
        user.setUser_img_name("undefined.jpg");
        loginService.insertUserProFile(user);

        request.setAttribute("user_name",user_t.getUser_name());
        request.setAttribute("user_pw",user_t.getUser_pw());
        return "login";
    }

    @RequestMapping("ajaxuploadimg")
    @ResponseBody
    public String ajaxUploadImg(HttpServletRequest request) throws IOException, ServletException {
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        //设置请求的编码格式
        request.setCharacterEncoding("UTF-8");
        //获取Part对象
        Part part=request.getPart("file");//表单中file文件域的name属性值
        //通过Part对象得到上传的文件名
        String filename=part.getSubmittedFileName();
        if(user.getUser_img_name().equals("undefined.jpg")){
            //得到文件存放的路径
            String filePath="E:/mydream/img/userimg/"+user.getUser_id();
            File uploadedFile= new File(filePath);
            if(!uploadedFile.exists()){
                uploadedFile.mkdir();
            }
            //上传文件到指定目录
            part.write(filePath+"/"+filename);
            user.setUser_img_name(filename);
            user.setUser_img_path("/userimg/"+user.getUser_id()+"/");
            loginService.updateUserProFile(user);
        }else{
            String filePath="E:/mydream/img/userimg/"+user.getUser_id()+filename;
            File uploadedFile= new File(filePath);
            uploadedFile.delete();
            filePath="E:/mydream/img/userimg/"+user.getUser_id();
            part.write(filePath+"/"+filename);
            user.setUser_img_name(filename);
            loginService.updateUserProFile(user);
        }
        session.setAttribute("user",user);
        return "/userimg/"+user.getUser_id()+"/"+filename;
    }

}
